//
//  CMBaseCtrl.m
//  CreditManger
//
//  Created by chaiyanan on 15/10/13.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBBaseCtrl.h"

@interface HBBaseCtrl ()
{
    BOOL _isFirst;
}

@end

@implementation HBBaseCtrl

-(void)addTap
{
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTouch:)]];
}

-(void)doTouch:(UIGestureRecognizer *)sender
{
    
}

-(void)doDone
{
    
}
//
-(void)buildLayout
{
    
    
}
-(void)showDoneWithTitle:(NSString *)title
{
    //右侧按钮
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,80,30)];
    rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightButton addTarget:self action:@selector(doDone)forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -15;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer, rightItem];
    
    self.navigationItem.rightBarButtonItem = rightItem;
    
//    self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleDone target:self action:@selector(doDone)];
}

- (void)showCustomNavWithColorString:(NSString*)colorString title:(NSString*)title titleFont:(UIFont*)font leftBackButtonShow:(BOOL)leftShow rightBtnShow:(BOOL)rightShow{
    UIView* navBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [navBack setBackgroundColor:[LCCommon getColor:@"000000"]];
    [self.view addSubview:navBack];
    
    UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH - 80, 44)];
    titleLabel.centerX = navBack.centerX;
    titleLabel.font = font;
    titleLabel.text = title;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [LCCommon getColor:@"ffffff"];
    [navBack addSubview:titleLabel];
    
    
    UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setFrame:CGRectMake(30, 30, 15, 15)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"bank_back"] forState:UIControlStateNormal];
    [navBack addSubview:leftBtn];
    [leftBtn addTarget:self action:@selector(leftbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)leftbtnClicked:(UIButton*)button{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 20, 30);
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"back_click"] forState:UIControlStateHighlighted];
    
    [leftBtn addTarget:self action:@selector(doBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    if (self.navigationController.viewControllers[0] != self) {
        leftBtn.imageEdgeInsets =UIEdgeInsetsMake(0, -15, 0, 0);
        self.navigationItem.leftBarButtonItem = leftItem;
    }
    
    self.extendedLayoutIncludesOpaqueBars = NO;
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    
    _isFirst = YES;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    if (_isFirst) {
        _isFirst = NO;
        [self buildLayout];
        [self makeNav];
    }
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

//重新设置navigationbar
-(void)makeNav{
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        
        NSArray *list=self.navigationController.navigationBar.subviews;
        
        for (id obj in list) {
            
            if ([obj isKindOfClass:[UIImageView class]]) {
                
                UIImageView *imageView=(UIImageView *)obj;
                
                NSArray *list2=imageView.subviews;
                
                for (id obj2 in list2) {
                    
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        
                        UIImageView *imageView2=(UIImageView *)obj2;
                        
                        imageView2.hidden=YES;
                    }
                }
            }
        }
    }
}

-(void)doBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Notification_ViewWillDisapper" object:nil];
    
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"Notification_ViewDidLayoutSubviews" object:nil];
}

@end
